package com.origon360.converters;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import com.origon.entities.Category;
import com.origon360.Services.CommonService;

@FacesConverter(value = "categoryConverter")
@ManagedBean(name="categoryConverter")
@SessionScoped
public class CategoryConverter implements Converter {
	
	@EJB
	private CommonService commonService;
	

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		if(value == null || value.isEmpty())
		{
			return null;
		}
		Category category = commonService.findCategoryByName(value);
		return category;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		if(value == null)
		{
			return null;
		}
		
		if(value instanceof Category)
		{
			return ((Category)value).getType();
		}
		return value.toString();
	}

}
